<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h1 class="title">登录</h1>
      <el-form-item prop="account">
        <el-input v-model="loginForm.account" type="text"  placeholder="账号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="login"
        >
        <span v-if="!loading">登 录</span>
        <span v-else>登 录 中...</span>

        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {loginApi} from "@/api/login"
import {getMenuApi} from "@/api/layout"
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        account: "admin",
        password: "666666",
        code: ""
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ]
      },
     loading: false
    };
  },
  created() {
    
  },
  methods: {
   login(){
    loginApi({
      "account": this.loginForm.account,
      "password": this.loginForm.password
    }).then(data=>{
      if(data.code == "200"){
        let data1 = data.data;
        window.localStorage.setItem("token",data1.token);
        window.localStorage.setItem("userName",data1.name);
        this.$store.state.token = data1.token;
        getMenuApi().then((data)=> {
        this.$store.state.menus = this.generateMenu(data.data);
        this.$router.push("/index");
        this.$message({
              type: 'success',
              message: '登录成功'
        });
      }).catch(()=>{
        this.Message.error({ message: "请求菜单异常"});
      })
      }
    })
   }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  position: fixed;
  background-image:  url('~@/assets/images/login-background.jpg');
  background-size: 100% 100%;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
</style>
